package com.jt.controller;

import com.jt.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller //将类交个springMVC容器管理,springMVC交给Spring容器管理
@ResponseBody  //将数据转化为"特殊的字符串"返回
@CrossOrigin  //允许跨域
public class UserController {
    /**
     * URL地址:http://localhost:8080/hello----- get 请求
     * http://localhost:8080/hello--- get 请求
     * http://localhost:8080/dog--- get 请求
     * http://localhost:8080/cat  --- get 请求
     * 参数: 无
     * 返回值:你好,springMVC"的字符串"
     *
     * */
    @RequestMapping("/hello")
    public String hello(){

        return "你好,SpringMVC";//如果出现404 就是请求路径不匹配
    }


    @RequestMapping("/cat")
    public String cat(){
        return "我是小猫猫!!";
    }
    @RequestMapping("/dog")
    public String dog(){
        return "我是小狗汪汪!!";
    }

    /**
     * 需求: 接收参数 name=xxx age=xx
     * URL:http://localhost:8080/findUserByNA?name=tomcat&age=18
     *
     *  知识点:
     *  1.通过url中的key获取数据
     *  2.如果参数众多,
     *  */
        @RequestMapping("/findUserByNA2")
    public String findUserByNA2(User user){
            return user.toString();
        }
        @RequestMapping("/hobby")
    public String hobby(String[] hobby){

            return Arrays.toString(hobby);
        }

  //      @RequestMapping("/findUser/{name}/{age}/{sex}")
//    public String findUser(@PathVariable String name,
//                           @PathVariable  int age,
//                           @PathVariable String sex){
//
//            return name+":"+age+":"+sex;
//        }
  @RequestMapping("/findUser/{name}/{age}/{sex}")
        public String findUser(User user){
            return user.toString();
        }

//http://localhost:8080/findJSON/?name=tomcat&age=18  网址
        @RequestMapping("/findJSON")
        @ResponseBody//将返回值转化为JSON
    public User findJSON(User user){
            user.setId(101);
            user.setSex("男");
            return user;
        }



}
